package com.dmap.admin.service.backgroundv3.Impl;

import com.dmap.admin.controller.backgroundv3.param.AppVersionEditParam;
import com.dmap.admin.controller.backgroundv3.param.AppVersionParam;
import com.dmap.admin.dao.AppVersionMapper;
import com.dmap.admin.po.AndroidVersion;
import com.dmap.admin.po.AppVersion;
import com.dmap.admin.po.AppVersionExample;
import com.dmap.admin.po.IosVersion;
import com.dmap.admin.service.backgroundv3.APPVersionService;
import com.dmap.admin.service.backgroundv3.AndroidVersionService;
import com.dmap.admin.service.backgroundv3.IOSVersionService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author: zmh
 * @create: 18-8-22
 * @description: app版本
 **/
@Service
public class APPVersionServiceImpl implements APPVersionService {
    @Resource
    private AppVersionMapper appVersionMapper;
    @Resource
    private AndroidVersionService androidVersionService;
    @Resource
    private IOSVersionService iosVersionService;

    @Transactional
    @Override
    public int insertAppVersion(AppVersionParam param) {
        AppVersion appVersion = new AppVersion();
        if(param.getAppNumber() != null){
            appVersion.setAppNumber(param.getAppNumber());
        }
        if(param.getAndroidVersion() != null){
            appVersion.setAndroidVersion(param.getAndroidVersion());
        }
        if(param.getIosVersion() != null){
            appVersion.setIosVersion(param.getIosVersion());
        }
        appVersion.setCreateTime(System.currentTimeMillis()/1000);
        appVersion.setUpdateTime(System.currentTimeMillis()/1000);
        return appVersionMapper.insertSelective(appVersion);
    }

    @Transactional
    @Override
    public int updateAppVersion(AppVersionEditParam param) {
        AppVersion appVersion = appVersionMapper.selectByPrimaryKey(Long.parseLong(param.getAppVersionId()));
        if (appVersion == null) {
            //没有该version
            return 2;
        }
        if(param.getAppNumber() != null){
            appVersion.setAppNumber(param.getAppNumber());
        }
        if(param.getAndroidVersion() != null){
            appVersion.setAndroidVersion(param.getAndroidVersion());
        }
        if(param.getIosVersion() != null){
            appVersion.setIosVersion(param.getIosVersion());
        }
        appVersion.setUpdateTime(System.currentTimeMillis()/1000);
        return appVersionMapper.updateByPrimaryKeySelective(appVersion);
    }

    @Override
    public int deleteAppVersion(long id) {
        AppVersion appVersion = appVersionMapper.selectByPrimaryKey(id);
        if (appVersion == null) {
            return 0;
        }
        //删除旧安卓版本、ios版本
        androidVersionService.deleteAndroidVersionByAppNumber(appVersion.getAppNumber());
        iosVersionService.deleteIosVersionByAppNumber(appVersion.getAppNumber());
        return appVersionMapper.deleteByPrimaryKey(id);
    }
    @Transactional
    @Override
    public int deleteAppVersionList(String ids) {
        List<String> appVersionList = Arrays.asList(ids.split(","));
        int count = 0;
        for(String id : appVersionList){
            //如果存在有最新的版本关联，就提示
            AppVersion appVersion = getAppVersion(Long.parseLong(id));
            if(StringUtils.isNotBlank(appVersion.getAndroidVersion()) || StringUtils.isNotBlank(appVersion.getIosVersion())){
                return 3;
            }
        }
        for (String id : appVersionList) {
            count += deleteAppVersion(Long.parseLong(id));
        }
        if (count == appVersionList.size()) {
            return 1;
        }
        return 0;
    }

    @Override
    public List<AppVersion> selectAppVersion() {
        AppVersionExample appVersionExample = new AppVersionExample();
        AppVersionExample.Criteria criteria = appVersionExample.createCriteria();
        //增加条件
        return appVersionMapper.selectByExample(appVersionExample);
    }

    @Override
    public int countAppVersion() {
        AppVersionExample appVersionExample = new AppVersionExample();
        AppVersionExample.Criteria criteria = appVersionExample.createCriteria();
        //增加条件
        return appVersionMapper.countByExample(appVersionExample);

    }

    @Override
    public AppVersion getAppVersion(long id) {
        return appVersionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateAppAndroidVersion(AndroidVersion androidVersion) {
        AppVersion appVersion = getAppVersionByAppNumber(androidVersion.getAppNumber());
        if(appVersion == null){
            //该appVersion不存在
            return 2 ;
        }
        if(androidVersion.getNewVersion() != null){
            appVersion.setAndroidVersion(androidVersion.getNewVersion());
        }
        return appVersionMapper.updateByPrimaryKey(appVersion);
    }

    @Override
    public int updateAppIosVersion(IosVersion iosVersion) {
        AppVersion appVersion = getAppVersionByAppNumber(iosVersion.getAppNumber());
        if(appVersion == null){
            //该appVersion不存在
            return 2 ;
        }
        if(iosVersion.getNewVersion() != null){
            appVersion.setIosVersion(iosVersion.getNewVersion());
        }
        return appVersionMapper.updateByPrimaryKey(appVersion);
    }


    @Override
    public int getAppUnique(String appNumber) {
        AppVersionExample appVersionExample = new AppVersionExample();
        AppVersionExample.Criteria criteria = appVersionExample.createCriteria();
        criteria.andAppNumberEqualTo(appNumber);
        int count = appVersionMapper.countByExample(appVersionExample);
        //1 代表存在  2 代表不存在
        return count == 0 ? 2 : 1;
    }

    @Override
    public AppVersion getAppVersionByAppNumber(String appNumber) {
        AppVersionExample appVersionExample = new AppVersionExample();
        AppVersionExample.Criteria criteria = appVersionExample.createCriteria();
        criteria.andAppNumberEqualTo(appNumber);
        List<AppVersion> appVersionList = appVersionMapper.selectByExample(appVersionExample);
       if(CollectionUtils.isEmpty(appVersionList)){
           return null;
       }
        return appVersionList.get(0);
    }
}
